package com.hd.crdao.wrapper;


import com.google.gson.Gson;
import com.hd.crdao.utils.MyToast;
import com.zhy.http.okhttp.callback.Callback;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 *
 *@author Programming
 *@date 2017/7/27 0027
 */

public abstract class CustomCallback<T> extends Callback<T> {


    @Override
    public void onError(Call call, Exception e, int id) {
        MyToast.show("网络开了个小差");
    }


    @Override
    public T parseNetworkResponse(Response response, int id) throws Exception {
        Class<T> entityClass = null;
        Type t = getClass().getGenericSuperclass();
        if (t instanceof ParameterizedType) {
            Type[] p = ((ParameterizedType) t).getActualTypeArguments();
            entityClass = (Class<T>) p[0];
        }
        if (entityClass != null) {
            return new Gson().fromJson(response.body().string(), entityClass);
        }
        return null;
    }
}

